package com.lzy.huijinjiazu.mvp.ui.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;

import com.flyco.tablayout.SlidingTabLayout;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.widget.autolayout.AutoToolbar;
import com.lzy.huijinjiazu.R;
import com.lzy.huijinjiazu.di.component.DaggerBalanceComponent;
import com.lzy.huijinjiazu.di.module.BalanceModule;
import com.lzy.huijinjiazu.mvp.contract.BalanceContract;
import com.lzy.huijinjiazu.mvp.presenter.BalancePresenter;
import com.lzy.huijinjiazu.mvp.ui.adapter.TabsAdapter;
import com.lzy.huijinjiazu.mvp.ui.fragment.BalaceDetailedFragment;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import butterknife.BindView;


public class BalanceActivity extends BaseActivity<BalancePresenter> implements BalanceContract.View, OnRefreshLoadmoreListener {
    @BindView(R.id.tabLayout)
    SlidingTabLayout mTabLayout;
    @BindView(R.id.viewPager)
    ViewPager mViewPager;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;

    private TabsAdapter mAdapter;

    @Override
    public void setupActivityComponent(AppComponent appComponent) {
        DaggerBalanceComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .balanceModule(new BalanceModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(Bundle savedInstanceState) {
        return R.layout.activity_balance; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mToolbar.getBackground().setAlpha(0);
        mPresenter.initTabLayout();
        mRefreshLayout.setOnRefreshLoadmoreListener(this);

    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.setTitleBarMarginTop(this, mToolbar);
    }

    @Override
    public void showTabLayout(String[] title, Fragment[] tabFragment) {
        mAdapter = new TabsAdapter(getSupportFragmentManager(), tabFragment, title);
        mViewPager.setAdapter(mAdapter);
        mTabLayout.setViewPager(mViewPager);
        mTabLayout.setCurrentTab(0);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                boolean isNoMore = ((BalaceDetailedFragment) mAdapter.getItem(position)).isNoMore();
                if (isNoMore) {
                    mRefreshLayout.finishLoadmoreWithNoMoreData();
                } else {
                    mRefreshLayout.resetNoMoreData();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        ((BalaceDetailedFragment) mAdapter.getItem(mViewPager.getCurrentItem())).onLoadmore(refreshlayout);
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        ((BalaceDetailedFragment) mAdapter.getItem(mViewPager.getCurrentItem())).onRefresh(refreshlayout);
    }
}
